﻿namespace UnitFundProfitability.BusinessLogicTest
{
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.Configuration;
	using System.Diagnostics;
	using System.Globalization;
	using System.Linq;
	using System.Reflection;
	using Microsoft.VisualStudio.TestTools.UnitTesting;
	using UnitFundProfitability.BusinessLogic;

	/// <summary>
	/// This is a test class for ManagerTest and is intended
	/// to contain all ManagerTest Unit Tests.
	/// </summary>
	[TestClass()]
	[DeploymentItem("database.sdf")]
	public class ManagerTest
	{
		private TestContext testContextInstance;

		/// <summary>
		/// Gets or sets the test context which provides
		/// information about and functionality for the current test run.
		/// </summary>
		/// <value>The test context.</value>
		public TestContext TestContext
		{
			get
			{
				return testContextInstance;
			}

			set
			{
				testContextInstance = value;
			}
		}

		/// <summary>
		/// A test fo CalculateFundsProfitabilites with exception.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithExceptionTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 20);
			DateTime endPeriod = new DateTime(2010, 10, 10);
			AssertException(() => Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod), typeof(ArgumentException));
		}

		/// <summary>
		/// A test for CalculateFundsProfitabilities.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 20);
			DateTime endPeriod = new DateTime(2010, 10, 22);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 7.51535M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 5.015M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 3.045M;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.4985742771684945164506480558M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 1.97M;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// Calculates the funds profitabilitites with markdown period.
		/// </summary>
		[TestMethod]
		public void CalculateFundsProfitabilititesWithMarkdownPeriod()
		{
			DateTime startPeriod = new DateTime(2010, 10, 20);
			DateTime endPeriod = new DateTime(2010, 12, 31);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company, 60);

			DateTime priceDate = new DateTime(2010, 10, 20);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			priceDate = priceDate.AddDays(70);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 7.56755M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 5.015M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 3.045M;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.50898305084745762711864406779661M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 1.97M;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for CalculateFundsProfitabilities.
		/// </summary>
		[TestMethod]
		public void CalculateFundsProfitabilitiesWithExchangeTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 20);
			DateTime endPeriod = new DateTime(2010, 10, 22);
			ManagementCompany company = AddManagementCompany();
			UnitFund sourceFund = AddUnitFund(company);
			UnitFund destinationFund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 21);
			AddMarkupRate(sourceFund);
			AddMarkupRate(destinationFund);
			decimal markdownRate = 0.015M;
			AddMarkdownRate(sourceFund, priceDate, markdownRate);
			AddMarkdownRate(destinationFund, priceDate, markdownRate);
			SetUnitPrice(sourceFund, priceDate, 1);
			SetUnitPrice(destinationFund, priceDate, 2);
			BuyUnits(sourceFund, priceDate, 1);
			BuyUnits(destinationFund, priceDate, 2);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(sourceFund, priceDate, 3);
			SetUnitPrice(destinationFund, priceDate, 4);
			Manager_Accessor.AddMovement(1, sourceFund, destinationFund, priceDate);
			IList<FundProfitability> profitabilities = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			FundProfitability actual =
				(from profitability in profitabilities
				 where profitability.FundId == destinationFund.FundId
				 select profitability).Single();
			Assert.AreEqual(7.06M, actual.OperationsAmount);
		}

		/// <summary>
		/// A test for CalculateFundsProfitabilities.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithNoPriceOrMarkdownRateInStartDateTest()
		{
			DateTime startPeriod = new DateTime(2010, 09, 20);
			DateTime endPeriod = new DateTime(2010, 10, 22);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 7.51535M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 5.075M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 5.075M;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.4808571428571428571428571429M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 0;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for CalculateFundsProfitabilities.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithNoUnitInStartDateTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 19);
			DateTime endPeriod = new DateTime(2010, 10, 22);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 7.51535M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 5.075M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 5.075M;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.4808571428571428571428571429M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 0;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for calculating profitability when there was no operation during period of report.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithNoOperationsInReportPeriodTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 20);
			DateTime endPeriod = new DateTime(2010, 10, 22);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 3.6917M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 1.97M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 0;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.8739593908629441624365482234M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 1.97M;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for calculating profitability when there was expenditure before period of report.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithExpenditureBeforeReportPeriodTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 21);
			DateTime endPeriod = new DateTime(2010, 10, 23);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 2);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			SellUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 5);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 4.54865M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 2.83475M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = 0;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 0.604603580562659846547314578M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 2.83475M;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for calculating profitability when there was expenditure in period of report.
		/// </summary>
		[TestMethod()]
		public void CalculateFundsProfitabilitiesWithExpenditureInReportPeriodTest()
		{
			DateTime startPeriod = new DateTime(2010, 10, 21);
			DateTime endPeriod = new DateTime(2010, 10, 23);
			ManagementCompany company = AddManagementCompany();
			UnitFund fund = AddUnitFund(company);
			DateTime priceDate = new DateTime(2010, 10, 19);
			AddMarkupRate(fund);
			AddMarkdownRate(fund, priceDate, 0.015M);
			SetUnitPrice(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 2);
			BuyUnits(fund, priceDate, 2);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 3);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 4);
			SellUnits(fund, priceDate, 1);
			priceDate = priceDate.AddDays(1);
			SetUnitPrice(fund, priceDate, 5);
			IList<FundProfitability> actual = Manager_Accessor.CalculateFundsProfitabilities(startPeriod, endPeriod);
			actual =
				(from profitability in actual
				 where profitability.FundId == fund.FundId
				 select profitability).ToList();
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);
			FundProfitability fundProfitability = actual.First();
			DateTime expectedEndDate = endPeriod;
			Assert.AreEqual(expectedEndDate, fundProfitability.EndDate);
			decimal expectedEndValue = 4.54865M;
			Assert.AreEqual(expectedEndValue, fundProfitability.EndValue);
			decimal expectedExpectedEndValue = 1.9778M;
			Assert.AreEqual(expectedExpectedEndValue, fundProfitability.ExpectedEndValue);
			string expectedFundName = fund.FundName;
			Assert.AreEqual(expectedFundName, fundProfitability.FundName);
			decimal expectedOperationsAmount = -3.6917M;
			Assert.AreEqual(expectedOperationsAmount, fundProfitability.OperationsAmount);
			decimal expectedProfitability = 1.2998533724340175953079178886M;
			Assert.AreEqual(expectedProfitability, fundProfitability.Profitability);
			DateTime expectedStartDate = startPeriod;
			Assert.AreEqual(expectedStartDate, fundProfitability.StartDate);
			decimal expectedStartValue = 5.6695M;
			Assert.AreEqual(expectedStartValue, fundProfitability.StartValue);
		}

		/// <summary>
		/// A test for SetMarkupRate.
		/// </summary>
		[TestMethod()]
		public void SetMarkupRateTest()
		{
			AssertException(() => Manager_Accessor.SetMarkupRate(null), typeof(ArgumentNullException));
			ManagementCompany company = new ManagementCompany();
			company.CompanyId = Guid.NewGuid();
			company.CompanyName = "CompanyName";
			Manager_Accessor.AddManagementCompany(company);
			UnitFund unitFund = new UnitFund();
			unitFund.FundId = Guid.NewGuid();
			unitFund.CompanyId = company.CompanyId;
			unitFund.FundName = "FundName";
			Manager_Accessor.AddUnitFund(unitFund);
			MarkupRate expectedMarkupRate = new MarkupRate();
			expectedMarkupRate.MarkupRateId = Guid.NewGuid();
			expectedMarkupRate.Date = new DateTime(2010, 10, 19);
			expectedMarkupRate.FundId = unitFund.FundId;
			expectedMarkupRate.Rate = -1;
			AssertException(() => Manager_Accessor.SetMarkupRate(expectedMarkupRate), typeof(ArgumentException));
			expectedMarkupRate.Rate = 2.3456M;
			Manager_Accessor.SetMarkupRate(expectedMarkupRate);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				MarkupRate actualMarkupRate =
					(from markupRate in dataContext.MarkupRates
					 where markupRate.FundId == unitFund.FundId
					 select markupRate).Take(1).First();
				Assert.AreEqual(expectedMarkupRate.Rate, actualMarkupRate.Rate);
			}
		}

		/// <summary>
		/// A test for SetMarkdownRate.
		/// </summary>
		[TestMethod()]
		public void SetMarkdownRateTest()
		{
			AssertException(() => Manager_Accessor.SetMarkdownRate(null), typeof(ArgumentNullException));
			ManagementCompany company = new ManagementCompany();
			company.CompanyId = Guid.NewGuid();
			company.CompanyName = "CompanyName";
			Manager_Accessor.AddManagementCompany(company);
			UnitFund unitFund = new UnitFund();
			unitFund.FundId = Guid.NewGuid();
			unitFund.CompanyId = company.CompanyId;
			unitFund.FundName = "FundName";
			Manager_Accessor.AddUnitFund(unitFund);
			MarkdownRate expectedMarkdownRate = new MarkdownRate();
			expectedMarkdownRate.MarkdownRateId = Guid.NewGuid();
			expectedMarkdownRate.Date = new DateTime(2010, 10, 19);
			expectedMarkdownRate.FundId = unitFund.FundId;
			expectedMarkdownRate.Rate = -1;
			AssertException(() => Manager_Accessor.SetMarkdownRate(expectedMarkdownRate), typeof(ArgumentException));
			expectedMarkdownRate.Rate = 2.3456M;
			Manager_Accessor.SetMarkdownRate(expectedMarkdownRate);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				MarkdownRate actualMarkdownRate =
					(from markupRate in dataContext.MarkdownRates
					 where markupRate.FundId == unitFund.FundId
					 select markupRate).Take(1).First();
				Assert.AreEqual(expectedMarkdownRate.Rate, actualMarkdownRate.Rate);
			}
		}

		/// <summary>
		/// A test for SetUnitPrice.
		/// </summary>
		[TestMethod()]
		public void SetUnitPriceTest()
		{
			AssertException(() => Manager_Accessor.SetUnitPrice(null), typeof(ArgumentNullException));
			ManagementCompany company = new ManagementCompany();
			company.CompanyId = Guid.NewGuid();
			company.CompanyName = "CompanyName";
			Manager_Accessor.AddManagementCompany(company);
			UnitFund unitFund = new UnitFund();
			unitFund.FundId = Guid.NewGuid();
			unitFund.CompanyId = company.CompanyId;
			unitFund.FundName = "FundName";
			Manager_Accessor.AddUnitFund(unitFund);
			UnitPrice expectedUnitPrice = new UnitPrice();
			expectedUnitPrice.UnitPriceId = Guid.NewGuid();
			expectedUnitPrice.PriceDate = new DateTime(2010, 10, 19);
			expectedUnitPrice.FundId = unitFund.FundId;
			expectedUnitPrice.Price = -1;
			AssertException(() => Manager_Accessor.SetUnitPrice(expectedUnitPrice), typeof(ArgumentException));
			expectedUnitPrice.Price = 2.3456M;
			Manager_Accessor.SetUnitPrice(expectedUnitPrice);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				UnitPrice actualUnitPrice =
					(from markupRate in dataContext.UnitPrices
					 where markupRate.FundId == unitFund.FundId
					 select markupRate).Take(1).First();
				Assert.AreEqual(expectedUnitPrice.Price, actualUnitPrice.Price);
			}
		}

		/// <summary>
		/// A test for AddUnitFund.
		/// </summary>
		[TestMethod()]
		public void AddUnitFundTest()
		{
			AssertException(() => Manager_Accessor.AddUnitFund(null), typeof(ArgumentNullException));
			ManagementCompany managementCompany = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(managementCompany);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Assert.IsNotNull(
					(from fund in dataContext.UnitFunds
					 where fund.FundId == unitFund.FundId
					 select fund).Single());
			}
		}

		/// <summary>
		/// A test for UpdateUnitFund.
		/// </summary>
		[TestMethod()]
		public void UpdateUnitFundTest()
		{
			AssertException(() => Manager.UpdateUnitFund(null), typeof(ArgumentNullException));
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company, 123);
			unitFund.FundName = "Changed Unit Fund";
			unitFund.MarkdownPeriod = 365;
			Manager.UpdateUnitFund(unitFund);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				UnitFund unitFundFromDatabase =
					(from item in dataContext.UnitFunds
					 where item.FundId == unitFund.FundId
					 select item).Single();
				Assert.AreEqual(unitFund.FundName, unitFundFromDatabase.FundName);
				Assert.AreEqual(unitFund.MarkdownPeriod, unitFundFromDatabase.MarkdownPeriod);
			}
		}

		/// <summary>
		/// A test for AddManagementCompany.
		/// </summary>
		[TestMethod()]
		public void AddManagementCompanyTest()
		{
			AssertException(() => Manager_Accessor.AddManagementCompany(null), typeof(ArgumentNullException));
			ManagementCompany addedManagementCompany = AddManagementCompany();
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Assert.IsNotNull(
					(from managementCompany in dataContext.ManagementCompanies
					 where managementCompany.CompanyId == addedManagementCompany.CompanyId
					 select managementCompany).Single());
			}
		}

		/// <summary>
		/// A test for CalculateExpenditures.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void CalculateExpendituresTest()
		{
			UnitFundsEntities dataContext = null;
			Manager_Accessor.ProfitabilityCalculatingHelper calculatingHelper = null;
			AssertException(() => Manager_Accessor.CalculateExpenditures(dataContext, calculatingHelper), typeof(NullReferenceException));
			using (dataContext = new UnitFundsEntities())
			{
				calculatingHelper = null;
				AssertException(() => Manager_Accessor.CalculateExpenditures(dataContext, calculatingHelper), typeof(NullReferenceException));
			}
			dataContext = null;
			calculatingHelper = new Manager_Accessor.ProfitabilityCalculatingHelper();
			AssertException(() => Manager_Accessor.CalculateExpenditures(dataContext, calculatingHelper), typeof(NullReferenceException));
		}

		/// <summary>
		/// A test for CalculateIncomes.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void CalculateIncomesTest()
		{
			UnitFundsEntities dataContext = null;
			Manager_Accessor.ProfitabilityCalculatingHelper calculatingHelper = null;
			AssertException(() => Manager_Accessor.CalculateIncomes(dataContext, calculatingHelper), typeof(NullReferenceException));
			using (dataContext = new UnitFundsEntities())
			{
				calculatingHelper = null;
				AssertException(() => Manager_Accessor.CalculateIncomes(dataContext, calculatingHelper), typeof(NullReferenceException));
			}
			dataContext = null;
			calculatingHelper = new Manager_Accessor.ProfitabilityCalculatingHelper();
			AssertException(() => Manager_Accessor.CalculateIncomes(dataContext, calculatingHelper), typeof(NullReferenceException));
		}

		/// <summary>
		/// A test for CreateFundProfitability.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void CreateFundProfitabilityTest()
		{
			Manager_Accessor.ProfitabilityCalculatingHelper calculatingHelper = new Manager_Accessor.ProfitabilityCalculatingHelper();
			UnitFund unitFund = new UnitFund();
			Guid expectedFundId = Guid.NewGuid();
			unitFund.FundId = expectedFundId;
			string expectedFundName = "ExpectedFundName";
			unitFund.FundName = expectedFundName;
			DateTime expectedEndDate = DateTime.Today;
			calculatingHelper.EndDate = expectedEndDate;
			DateTime expectedStartDate = expectedEndDate.AddDays(-1);
			calculatingHelper.StartDate = expectedStartDate;
			FundProfitability actual = Manager_Accessor.CreateFundProfitability(calculatingHelper, unitFund);
			Assert.AreEqual(expectedFundId, actual.FundId);
			Assert.AreEqual(expectedFundName, actual.FundName);
			Assert.AreEqual(expectedStartDate, actual.StartDate);
			Assert.AreEqual(expectedEndDate, actual.EndDate);
		}

		/// <summary>
		/// A test for CreateFundSnapshots.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void CreateFundSnapshotsTest()
		{
			ManagementCompany company = AddManagementCompany();
			UnitFund firstUnitFund = AddUnitFund(company);
			UnitFund secondUnitFund = AddUnitFund(company);
			DateTime date = DateTime.Today;
			Random randomizer = new Random(DateTime.Now.Millisecond);
			decimal markdownRateOfFirstFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(firstUnitFund, date, markdownRateOfFirstFund);
			decimal priceOfFirstFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(firstUnitFund, date, priceOfFirstFund);
			decimal markdownRateOfSecondFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(secondUnitFund, date, markdownRateOfSecondFund);
			decimal priceOfSecondFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(secondUnitFund, date, priceOfSecondFund);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				IQueryable<IGrouping<Guid, UnitPrice>> unitPricesGroups = Manager_Accessor.GetUnitPrices(date, dataContext);
				IQueryable<IGrouping<Guid, MarkdownRate>> markdownRatesGroups = Manager_Accessor.GetMarkdownRates(date, dataContext);
				IList<UnitFund> unitFunds = Manager_Accessor.GetUnitFunds();
				MethodInfo method = Manager_Accessor.ShadowedType.ReferencedType.GetMethod("CreateFundSnapshots", BindingFlags.Static | BindingFlags.NonPublic);
				IDictionary fundSnapshots = method.Invoke(null, new object[] { date, unitFunds, unitPricesGroups, markdownRatesGroups }) as IDictionary;
				bool hasDataForFirstUnitFund = false;
				bool hasDataForSecondUnitFund = false;
				foreach (Guid unitFund in fundSnapshots.Keys)
				{
					Manager_Accessor.FundSnapshot fundSnapshot = new Manager_Accessor.FundSnapshot(new PrivateObject(fundSnapshots[unitFund]));
					if (unitFund == firstUnitFund.FundId)
					{
						hasDataForFirstUnitFund = true;
						Assert.AreEqual(markdownRateOfFirstFund, fundSnapshot.MarkdownRate);
						Assert.AreEqual(priceOfFirstFund, fundSnapshot.UnitPrice);
					}
					if (unitFund == secondUnitFund.FundId)
					{
						hasDataForSecondUnitFund = true;
						Assert.AreEqual(markdownRateOfSecondFund, fundSnapshot.MarkdownRate);
						Assert.AreEqual(priceOfSecondFund, fundSnapshot.UnitPrice);
					}
				}
				Assert.IsTrue(hasDataForFirstUnitFund);
				Assert.IsTrue(hasDataForSecondUnitFund);
			}
		}

		/// <summary>
		/// A test for GetMarkdownRates.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetMarkdownRatesTest()
		{
			ManagementCompany company = AddManagementCompany();
			UnitFund firstUnitFund = AddUnitFund(company);
			UnitFund secondUnitFund = AddUnitFund(company);
			DateTime markdownRateDate = DateTime.Today.AddDays(-1);
			Random randomizer = new Random(DateTime.Now.Millisecond);
			decimal firstMarkdownRateOfFirstFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(firstUnitFund, markdownRateDate, firstMarkdownRateOfFirstFund);
			decimal firstMarkdownRateOfSecondFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(secondUnitFund, markdownRateDate, firstMarkdownRateOfSecondFund);
			markdownRateDate = markdownRateDate.AddDays(1);
			decimal secondMarkdownRateOfFirstFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(firstUnitFund, markdownRateDate, secondMarkdownRateOfFirstFund);
			decimal secondMarkdownRateOfSecondFund = (decimal)randomizer.NextDouble();
			AddMarkdownRate(secondUnitFund, markdownRateDate, secondMarkdownRateOfSecondFund);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				IQueryable<IGrouping<Guid, MarkdownRate>> actualRates = Manager_Accessor.GetMarkdownRates(DateTime.Today, dataContext);
				bool hasRatesForFirstFund = false;
				bool hasRatesForSecondFund = false;
				foreach (IGrouping<Guid, MarkdownRate> ratesByUnitFund in actualRates)
				{
					if (ratesByUnitFund.Key == firstUnitFund.FundId)
					{
						hasRatesForFirstFund = true;
						Assert.IsNotNull(ratesByUnitFund.Where((markdownRate) => markdownRate.Rate == firstMarkdownRateOfFirstFund).Single());
						Assert.IsNotNull(ratesByUnitFund.Where((markdownRate) => markdownRate.Rate == secondMarkdownRateOfFirstFund).Single());
					}
					if (ratesByUnitFund.Key == secondUnitFund.FundId)
					{
						hasRatesForSecondFund = true;
						Assert.IsNotNull(ratesByUnitFund.Where((markdownRate) => markdownRate.Rate == firstMarkdownRateOfSecondFund).Single());
						Assert.IsNotNull(ratesByUnitFund.Where((markdownRate) => markdownRate.Rate == secondMarkdownRateOfSecondFund).Single());
					}
				}
				Assert.IsTrue(hasRatesForFirstFund);
				Assert.IsTrue(hasRatesForSecondFund);
			}
		}

		/// <summary>
		/// A test for GetUnitPrices.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetUnitPricesTest()
		{
			ManagementCompany company = AddManagementCompany();
			UnitFund firstUnitFund = AddUnitFund(company);
			UnitFund secondUnitFund = AddUnitFund(company);
			DateTime priceDate = DateTime.Today.AddDays(-1);
			Random randomizer = new Random(DateTime.Now.Millisecond);
			decimal firstPriceOfFirstFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(firstUnitFund, priceDate, firstPriceOfFirstFund);
			decimal firstPriceOfSecondFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(secondUnitFund, priceDate, firstPriceOfSecondFund);
			priceDate = priceDate.AddDays(1);
			decimal secondPriceOfFirstFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(firstUnitFund, priceDate, secondPriceOfFirstFund);
			decimal secondPriceOfSecondFund = (decimal)randomizer.NextDouble();
			SetUnitPrice(secondUnitFund, priceDate, secondPriceOfSecondFund);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				IQueryable<IGrouping<Guid, UnitPrice>> actualPrices = Manager_Accessor.GetUnitPrices(DateTime.Today, dataContext);
				bool hasPricesForFirstFund = false;
				bool hasPricesForSecondFund = false;
				foreach (IGrouping<Guid, UnitPrice> pricesByUnitFund in actualPrices)
				{
					if (pricesByUnitFund.Key == firstUnitFund.FundId)
					{
						hasPricesForFirstFund = true;
						Assert.IsNotNull(pricesByUnitFund.Where((price) => price.Price == firstPriceOfFirstFund).Single());
						Assert.IsNotNull(pricesByUnitFund.Where((price) => price.Price == secondPriceOfFirstFund).Single());
					}
					if (pricesByUnitFund.Key == secondUnitFund.FundId)
					{
						hasPricesForSecondFund = true;
						Assert.IsNotNull(pricesByUnitFund.Where((price) => price.Price == firstPriceOfSecondFund).Single());
						Assert.IsNotNull(pricesByUnitFund.Where((price) => price.Price == secondPriceOfSecondFund).Single());
					}
				}
				Assert.IsTrue(hasPricesForFirstFund);
				Assert.IsTrue(hasPricesForSecondFund);
			}
		}

		/// <summary>
		/// A test for GetManagementCompanies.
		/// </summary>
		[TestMethod()]
		public void GetManagementCompaniesTest()
		{
			ManagementCompany firstManagementCompany = AddManagementCompany();
			ManagementCompany secondManagementCompany = AddManagementCompany();
			IList<ManagementCompany> actual = Manager_Accessor.GetManagementCompanies();
			bool hasFirstManagementCompany = false;
			bool hasSecondManagementCompany = false;
			foreach (ManagementCompany company in actual)
			{
				if (company.CompanyId == firstManagementCompany.CompanyId)
				{
					hasFirstManagementCompany = true;
				}
				if (company.CompanyId == secondManagementCompany.CompanyId)
				{
					hasSecondManagementCompany = true;
				}
			}
			Assert.IsTrue(hasFirstManagementCompany);
			Assert.IsTrue(hasSecondManagementCompany);
		}

		/// <summary>
		/// A test for GetUnitFunds.
		/// </summary>
		[TestMethod()]
		public void GetUnitFundsWithParameterTest()
		{
			AssertException(() => Manager_Accessor.GetUnitFunds(null), typeof(ArgumentNullException));
			ManagementCompany firstManagementCompany = AddManagementCompany();
			ManagementCompany secondManagementCompany = AddManagementCompany();
			UnitFund firstUnitFundOfFirstCompany = AddUnitFund(firstManagementCompany);
			UnitFund secondUnitFundOfFirstCompany = AddUnitFund(firstManagementCompany);
			UnitFund firstUnitFundOfSecondCompany = AddUnitFund(secondManagementCompany);
			bool hasFirstUnitFund = false;
			bool hasSecondUnitFund = false;
			bool hasUnitFundsOfWrongCompany = false;
			foreach (UnitFund unitFund in Manager_Accessor.GetUnitFunds(firstManagementCompany))
			{
				if (unitFund.FundId == firstUnitFundOfFirstCompany.FundId)
				{
					hasFirstUnitFund = true;
				}
				if (unitFund.FundId == secondUnitFundOfFirstCompany.FundId)
				{
					hasSecondUnitFund = true;
				}
				if (unitFund.CompanyId != firstManagementCompany.CompanyId)
				{
					hasUnitFundsOfWrongCompany = true;
				}
			}
			Assert.IsTrue(hasFirstUnitFund);
			Assert.IsTrue(hasSecondUnitFund);
			Assert.IsFalse(hasUnitFundsOfWrongCompany);
		}

		/// <summary>
		/// A test for GetUnitFunds.
		/// </summary>
		[TestMethod()]
		public void GetUnitFundsTest()
		{
			ManagementCompany firstManagementCompany = AddManagementCompany();
			ManagementCompany secondManagementCompany = AddManagementCompany();
			UnitFund firstUnitFund = AddUnitFund(firstManagementCompany);
			UnitFund secondUnitFund = AddUnitFund(secondManagementCompany);
			bool hasFirstUnitFund = false;
			bool hasSecondUnitFund = false;
			foreach (UnitFund unitFund in Manager_Accessor.GetUnitFunds())
			{
				if (unitFund.FundId == firstUnitFund.FundId)
				{
					hasFirstUnitFund = true;
				}
				if (unitFund.FundId == secondUnitFund.FundId)
				{
					hasSecondUnitFund = true;
				}
			}
			Assert.IsTrue(hasFirstUnitFund);
			Assert.IsTrue(hasSecondUnitFund);
		}

		/// <summary>
		/// A test for AddIncome.
		/// </summary>
		[TestMethod()]
		public void AddIncomeTest()
		{
			DateTime incomeDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			AddMarkupRate(unitFund, incomeDate.AddDays(-2), 3);
			AddMarkupRate(unitFund, incomeDate.AddDays(-1), 0.015M);
			AddMarkupRate(unitFund, incomeDate, 3);
			SetUnitPrice(unitFund, incomeDate.AddDays(-1), 3);
			SetUnitPrice(unitFund, incomeDate, 2);
			SetUnitPrice(unitFund, incomeDate.AddDays(1), 3);
			Income income = new Income();
			income.FundId = unitFund.FundId;
			AssertException(() => Manager_Accessor.AddIncome(income, incomeDate), typeof(InvalidOperationException));
			income.MoneyAmount = 1;
			income.UnitsAmount = 1;
			AssertException(() => Manager_Accessor.AddIncome(income, incomeDate), typeof(InvalidOperationException));
			income.MoneyAmount = -1;
			income.UnitsAmount = 0;
			AssertException(() => Manager_Accessor.AddIncome(income, incomeDate), typeof(InvalidOperationException));
			income.MoneyAmount = 0;
			income.UnitsAmount = -1;
			AssertException(() => Manager_Accessor.AddIncome(income, incomeDate), typeof(InvalidOperationException));
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				income.MoneyAmount = 4;
				income.UnitsAmount = 0;
				Manager_Accessor.AddIncome(income, incomeDate);
				Income addedIncome =
					(from item in dataContext.Incomes
					 where item.IncomeId == income.IncomeId
					 select item).Single();
				Assert.AreEqual(1.9704433497536945812807881774M, income.UnitsAmount);
				income = new Income();
				income.FundId = unitFund.FundId;
				income.MoneyAmount = 0;
				income.UnitsAmount = 2;
				Manager_Accessor.AddIncome(income, incomeDate);
				addedIncome =
					(from item in dataContext.Incomes
					 where item.IncomeId == income.IncomeId
					 select item).Single();
				Assert.AreEqual(4.06M, income.MoneyAmount);
			}
		}

		/// <summary>
		/// A test for AddIncome with pending income.
		/// </summary>
		[TestMethod]
		public void AddPendingIncomeTest()
		{
			DateTime incomeDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			AddMarkupRate(unitFund, incomeDate.AddMonths(-1), 0);
			Income income = new Income();
			income.FundId = unitFund.FundId;
			income.MoneyAmount = 4;
			income.UnitsAmount = 0;
			Manager_Accessor.AddIncome(income, incomeDate);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Operation addedOperation =
					(from item in dataContext.Operations
					 where item.OperationId == income.IncomeId
					 select item).Single();
				Income addedIncome =
					(from item in dataContext.Incomes
					 where item.IncomeId == income.IncomeId
					 select item).Single();
				Assert.IsTrue(addedOperation.IsPendingOperation);
				Assert.AreEqual(incomeDate, addedOperation.OperationDate);
				Assert.AreEqual(income.MoneyAmount, addedIncome.MoneyAmount);
				Assert.AreEqual(income.UnitsAmount, addedIncome.UnitsAmount);
			}
			IList<FundProfitability> fundsProfitabilites = Manager_Accessor.CalculateFundsProfitabilities(incomeDate.AddMonths(-1), incomeDate);
			FundProfitability fundProfitability =
				(from item in fundsProfitabilites
				 where item.FundId == unitFund.FundId
				 select item).SingleOrDefault();
			Assert.IsNull(fundProfitability);
		}

		/// <summary>
		/// A test for AddExpenditure.
		/// </summary>
		[TestMethod()]
		public void AddExpenditureTest()
		{
			DateTime expenditureDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			AddMarkupRate(unitFund, expenditureDate.AddMonths(-2), 0.015M);
			AddMarkdownRate(unitFund, expenditureDate.AddMonths(-2), 0.015M);
			SetUnitPrice(unitFund, expenditureDate.AddDays(-8), 1);
			SetUnitPrice(unitFund, expenditureDate.AddDays(-2), 3);
			SetUnitPrice(unitFund, expenditureDate.AddDays(-1), 2);
			SetUnitPrice(unitFund, expenditureDate, 3);
			Income income = new Income();
			income.FundId = unitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, expenditureDate.AddDays(-10));
			Guid firstIncomeId = income.IncomeId;
			income = new Income();
			income.FundId = unitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, expenditureDate.AddDays(-9));
			Guid secondIncomeId = income.IncomeId;
			income = new Income();
			income.FundId = unitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, expenditureDate.AddDays(-8));
			Guid thirdIncomeId = income.IncomeId;
			Expenditure expenditure = new Expenditure();
			expenditure.FundId = unitFund.FundId;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			expenditure.UnitsAmount = 1;
			expenditure.MoneyAmount = 1;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			expenditure.MoneyAmount = 0;
			expenditure.UnitsAmount = 10;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			expenditure.MoneyAmount = 100;
			expenditure.UnitsAmount = 0;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			expenditure.MoneyAmount = -1;
			expenditure.UnitsAmount = 0;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			expenditure.MoneyAmount = 0;
			expenditure.UnitsAmount = -1;
			AssertException(() => Manager_Accessor.AddExpenditure(expenditure, expenditureDate), typeof(InvalidOperationException));
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				expenditure.MoneyAmount = 0;
				expenditure.UnitsAmount = 3;
				Manager_Accessor.AddExpenditure(expenditure, expenditureDate);
				Expenditure addedExpenditure =
					(from item in dataContext.Expenditures
					 where item.ExpenditureId == expenditure.ExpenditureId
					 select item).Single();
				Assert.AreEqual(8.1084M, addedExpenditure.MoneyAmount);
				expenditure = new Expenditure();
				expenditure.FundId = unitFund.FundId;
				expenditure.UnitsAmount = 0;
				expenditure.MoneyAmount = 4;
				Manager_Accessor.AddExpenditure(expenditure, expenditureDate);
				addedExpenditure =
					(from item in dataContext.Expenditures
					 where item.ExpenditureId == expenditure.ExpenditureId
					 select item).Single();
				Assert.AreEqual(1.47994672191801095M, addedExpenditure.UnitsAmount);
				Income incomeToCheck =
					(from item in dataContext.Incomes
					 where item.IncomeId == firstIncomeId
					 select item).Single();
				Assert.AreEqual(incomeToCheck.UnitsAmount, incomeToCheck.UnitsExpenditureAmount);
				incomeToCheck =
					(from item in dataContext.Incomes
					 where item.IncomeId == secondIncomeId
					 select item).Single();
				Assert.AreEqual(incomeToCheck.UnitsAmount, incomeToCheck.UnitsExpenditureAmount);
				incomeToCheck =
					(from item in dataContext.Incomes
					 where item.IncomeId == thirdIncomeId
					 select item).Single();
				Assert.AreEqual(0.47994672191801095M, incomeToCheck.UnitsExpenditureAmount);
			}
		}

		/// <summary>
		/// A test for AddMovement.
		/// </summary>
		[TestMethod()]
		public void AddMovementTest()
		{
			DateTime movementDate = DateTime.Today;
			ManagementCompany firstCompany = AddManagementCompany();
			UnitFund sourceUnitFund = AddUnitFund(firstCompany);
			UnitFund destinationUnitFund = AddUnitFund(firstCompany);
			ManagementCompany secondCompany = AddManagementCompany();
			UnitFund incorrectDestinationUnitFund = AddUnitFund(secondCompany);
			AddMarkupRate(sourceUnitFund, movementDate.AddMonths(-2), 0.015M);
			AddMarkdownRate(sourceUnitFund, movementDate.AddMonths(-2), 0.015M);
			AddMarkupRate(destinationUnitFund, movementDate.AddMonths(-2), 0.015M);
			AddMarkdownRate(destinationUnitFund, movementDate.AddMonths(-2), 0.015M);
			SetUnitPrice(sourceUnitFund, movementDate.AddDays(-8), 1);
			SetUnitPrice(sourceUnitFund, movementDate.AddDays(-2), 3);
			SetUnitPrice(sourceUnitFund, movementDate.AddDays(-1), 2);
			SetUnitPrice(sourceUnitFund, movementDate, 4);
			SetUnitPrice(destinationUnitFund, movementDate.AddDays(-8), 1);
			SetUnitPrice(destinationUnitFund, movementDate.AddDays(-2), 3);
			SetUnitPrice(destinationUnitFund, movementDate.AddDays(-1), 5);
			SetUnitPrice(destinationUnitFund, movementDate, 6);
			Income income = new Income();
			income.FundId = sourceUnitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, movementDate.AddDays(-10));
			Guid firstIncomeId = income.IncomeId;
			income = new Income();
			income.FundId = sourceUnitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, movementDate.AddDays(-9));
			Guid secondIncomeId = income.IncomeId;
			income = new Income();
			income.FundId = sourceUnitFund.FundId;
			income.UnitsAmount = 2;
			Manager_Accessor.AddIncome(income, movementDate.AddDays(-8));
			AssertException(() => Manager_Accessor.AddMovement(-1, sourceUnitFund, destinationUnitFund, movementDate), typeof(ArgumentException));
			AssertException(() => Manager_Accessor.AddMovement(1, sourceUnitFund, incorrectDestinationUnitFund, movementDate), typeof(InvalidOperationException));
			AssertException(() => Manager_Accessor.AddMovement(100, sourceUnitFund, destinationUnitFund, movementDate), typeof(InvalidOperationException));
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Manager_Accessor.AddMovement(2.5M, sourceUnitFund, destinationUnitFund, movementDate);
				Expenditure addedExpenditure =
					(from item in dataContext.Expenditures
					 where item.FundId == sourceUnitFund.FundId
					 select item).Single();
				Assert.AreEqual(2.5M, addedExpenditure.UnitsAmount);
				Assert.AreEqual(10, addedExpenditure.MoneyAmount);
				Income incomeToCheck =
					(from item in dataContext.Incomes
					 where item.FundId == destinationUnitFund.FundId
					 select item).Single();
				Assert.AreEqual(2.5375M, incomeToCheck.MoneyAmount);
				Assert.AreEqual(1.66666666666666667M, incomeToCheck.UnitsAmount);
				incomeToCheck =
					(from item in dataContext.Incomes
					 where item.IncomeId == firstIncomeId
					 select item).Single();
				Assert.AreEqual(incomeToCheck.UnitsAmount, incomeToCheck.UnitsExpenditureAmount);
				incomeToCheck =
					(from item in dataContext.Incomes
					 where item.IncomeId == secondIncomeId
					 select item).Single();
				Assert.AreEqual(.5M, incomeToCheck.UnitsExpenditureAmount);
			}
		}

		/// <summary>
		/// A test for AdjustIncomeUnitsAmount.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void AdjustIncomeUnitsAmountTest()
		{
			Income income = new Income();
			income.UnitsAmount = 10;
			MarkupRate markupRate = new MarkupRate();
			markupRate.Rate = 0.015M;
			UnitPrice price = new UnitPrice();
			price.Price = 1;
			Manager_Accessor.AdjustIncomeUnitsAmount(income, markupRate, price);
			Assert.AreEqual(10.15M, income.MoneyAmount);
			income = new Income();
			income.MoneyAmount = 101.5M;
			Manager_Accessor.AdjustIncomeUnitsAmount(income, markupRate, price);
			Assert.AreEqual(100, income.UnitsAmount);
		}

		/// <summary>
		/// A test for CalculatePossibleExpenditure.
		/// </summary>
		[TestMethod()]
		public void CalculatePossibleExpenditureTest()
		{
			decimal unitPrice = 2;
			decimal unitsCount = 100;
			decimal markdownRate = .015M;
			decimal incomeUnitPrice = 1;
			decimal expected = 184.39M;
			DateTime incomeDate = DateTime.Now;
			DateTime calculationDate = incomeDate.AddDays(3);
			int markdownPeriod = 365;
			decimal actual = Manager_Accessor.CalculatePossibleExpenditure(
				unitPrice,
				unitsCount,
				markdownRate,
				incomeUnitPrice,
				incomeDate,
				calculationDate,
				markdownPeriod);
			Assert.AreEqual(expected, actual);
		}

		/// <summary>
		/// A test for GetActiveIncomes.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetActiveIncomesTest()
		{
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				DateTime incomeDate = DateTime.Today;
				ManagementCompany company = AddManagementCompany();
				UnitFund unitFund = AddUnitFund(company);
				AddMarkupRate(unitFund, incomeDate.AddDays(-1), 0.015M);
				AddMarkdownRate(unitFund, incomeDate.AddDays(-1), 0.015M);
				SetUnitPrice(unitFund, incomeDate, 2);
				SetUnitPrice(unitFund, incomeDate.AddDays(1), 3);
				SetUnitPrice(unitFund, incomeDate.AddDays(2), 3);
				Income income = new Income();
				income.FundId = unitFund.FundId;
				income.UnitsAmount = 10;
				Manager_Accessor.AddIncome(income, incomeDate);
				income = new Income();
				income.FundId = unitFund.FundId;
				income.UnitsAmount = 10;
				Manager_Accessor.AddIncome(income, incomeDate.AddDays(1));
				DateTime finalDate = incomeDate.AddDays(3);
				Guid unitFundId = unitFund.FundId;
				IEnumerable<Income> actual = Manager_Accessor.GetActiveIncomes(finalDate, dataContext, unitFundId);
				Assert.AreEqual(2, actual.Count());
				Expenditure expenditure = new Expenditure();
				expenditure.FundId = unitFund.FundId;
				expenditure.UnitsAmount = 10;
				Manager_Accessor.AddExpenditure(expenditure, incomeDate.AddDays(2));
				actual = Manager_Accessor.GetActiveIncomes(finalDate, dataContext, unitFundId);
				Assert.AreEqual(1, actual.Count());
			}
		}

		/// <summary>
		/// A test for GetFirstPrice.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetFirstPriceTest()
		{
			DateTime incomeDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			SetUnitPrice(unitFund, incomeDate, 1);
			SetUnitPrice(unitFund, incomeDate.AddDays(1), 2);
			SetUnitPrice(unitFund, incomeDate.AddDays(2), 3);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				UnitPrice actual = Manager_Accessor.GetFirstPrice(incomeDate.AddDays(1), dataContext, unitFund.FundId);
				Assert.AreEqual(2, actual.Price);
			}
		}

		/// <summary>
		/// A test for GetLastMarkdownRate.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetLastMarkdownRateTest()
		{
			DateTime markdownRateDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			AddMarkdownRate(unitFund, markdownRateDate, 1);
			AddMarkdownRate(unitFund, markdownRateDate.AddDays(1), 2);
			AddMarkdownRate(unitFund, markdownRateDate.AddDays(2), 3);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				MarkdownRate actual = Manager_Accessor.GetLastMarkdownRate(markdownRateDate.AddDays(1), dataContext, unitFund.FundId);
				Assert.AreEqual(1, actual.Rate);
			}
		}

		/// <summary>
		/// A test for GetLastMarkupRate.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void GetLastMarkupRateTest()
		{
			DateTime markupRateDate = DateTime.Today;
			ManagementCompany company = AddManagementCompany();
			UnitFund unitFund = AddUnitFund(company);
			AddMarkupRate(unitFund, markupRateDate, 1);
			AddMarkupRate(unitFund, markupRateDate.AddDays(1), 2);
			AddMarkupRate(unitFund, markupRateDate.AddDays(2), 3);
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				MarkupRate actual = Manager_Accessor.GetLastMarkupRate(markupRateDate.AddDays(1), dataContext, unitFund.FundId);
				Assert.AreEqual(1, actual.Rate);
			}
		}

		/// <summary>
		/// A test for WriteOffMoneyAmount.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void WriteOffMoneyAmountTest()
		{
			Expenditure expenditure = new Expenditure();
			expenditure.MoneyAmount = 100;
			DateTime expenditureDate = DateTime.Now.AddDays(3);
			MarkdownRate markdownRate = new MarkdownRate();
			markdownRate.Rate = 0.015M;
			UnitPrice price = new UnitPrice();
			price.Price = 2;
			List<Income> incomes = new List<Income>();
			IEnumerator<Income> incomesEnumerator = incomes.GetEnumerator();
			AssertException(
				() => Manager_Accessor.WriteOffMoneyAmount(expenditure, expenditureDate, markdownRate, price, incomesEnumerator),
				typeof(InvalidOperationException));
			Income firstIncome = new Income();
			firstIncome.MoneyAmount = 25;
			firstIncome.UnitsAmount = 25;
			firstIncome.Operation = new Operation();
			firstIncome.Operation.OperationDate = DateTime.Now;
			UnitFund unitFund = new UnitFund();
			unitFund.MarkdownPeriod = 365;
			firstIncome.UnitFund = unitFund;
			incomes.Add(firstIncome);
			Income secondIncome = new Income();
			secondIncome.MoneyAmount = 100;
			secondIncome.UnitsAmount = 50;
			secondIncome.Operation = new Operation();
			secondIncome.Operation.OperationDate = DateTime.Now;
			secondIncome.UnitFund = unitFund;
			incomes.Add(secondIncome);
			incomesEnumerator = incomes.GetEnumerator();
			Manager_Accessor.WriteOffMoneyAmount(expenditure, expenditureDate, markdownRate, price, incomesEnumerator);
			Assert.AreEqual(25, firstIncome.UnitsExpenditureAmount);
			Assert.AreEqual(27.361675126903553299492385787M, secondIncome.UnitsExpenditureAmount);
			Assert.AreEqual(52.361675126903553299492385787M, expenditure.UnitsAmount);
		}

		/// <summary>
		/// A test for WriteOffMoneyAmountForMovement.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void WriteOffUnitsAmountForMovementTest()
		{
			decimal unitsAmount = 50;
			UnitPrice sourceUnitPrice = new UnitPrice();
			sourceUnitPrice.Price = 2;
			UnitPrice destinationUnitPrice = new UnitPrice();
			destinationUnitPrice.Price = 1;
			List<Income> incomes = new List<Income>();
			IEnumerator<Income> incomesEnumerator = incomes.GetEnumerator();
			Income income = new Income();
			AssertException(
				() => Manager_Accessor.WriteOffUnitsAmountForMovement(unitsAmount, sourceUnitPrice, destinationUnitPrice, incomesEnumerator, income),
				typeof(InvalidOperationException));
			Income firstIncome = new Income();
			firstIncome.MoneyAmount = 25;
			firstIncome.UnitsAmount = 25;
			incomes.Add(firstIncome);
			Income secondIncome = new Income();
			secondIncome.MoneyAmount = 100;
			secondIncome.UnitsAmount = 50;
			incomes.Add(secondIncome);
			incomesEnumerator = incomes.GetEnumerator();
			Manager_Accessor.WriteOffUnitsAmountForMovement(unitsAmount, sourceUnitPrice, destinationUnitPrice, incomesEnumerator, income);
			Assert.AreEqual(25, firstIncome.UnitsExpenditureAmount);
			Assert.AreEqual(25, secondIncome.UnitsExpenditureAmount);
			Assert.AreEqual(75, income.MoneyAmount);
			Assert.AreEqual(100, income.UnitsAmount);
		}

		/// <summary>
		/// A test for WriteOffUnitsAmount.
		/// </summary>
		[TestMethod()]
		[DeploymentItem("UnitFundProfitability.BusinessLogic.dll")]
		public void WriteOffUnitsAmountTest()
		{
			Expenditure expenditure = new Expenditure();
			expenditure.UnitsAmount = 50;
			DateTime expenditureDate = DateTime.Now.AddDays(3);
			MarkdownRate markdownRate = new MarkdownRate();
			markdownRate.Rate = 0.015M;
			UnitPrice price = new UnitPrice();
			price.Price = 2;
			List<Income> incomes = new List<Income>();
			IEnumerator<Income> incomesEnumerator = incomes.GetEnumerator();
			AssertException(
				() => Manager_Accessor.WriteOffUnitsAmount(expenditure, expenditureDate, markdownRate, price, incomesEnumerator),
				typeof(InvalidOperationException));
			Income firstIncome = new Income();
			firstIncome.MoneyAmount = 25;
			firstIncome.UnitsAmount = 25;
			firstIncome.Operation = new Operation();
			firstIncome.Operation.OperationDate = DateTime.Now;
			UnitFund unitFund = new UnitFund();
			unitFund.MarkdownPeriod = 365;
			firstIncome.UnitFund = unitFund;
			incomes.Add(firstIncome);
			Income secondIncome = new Income();
			secondIncome.MoneyAmount = 100;
			secondIncome.UnitsAmount = 50;
			secondIncome.Operation = new Operation();
			secondIncome.Operation.OperationDate = DateTime.Now;
			secondIncome.UnitFund = unitFund;
			incomes.Add(secondIncome);
			incomesEnumerator = incomes.GetEnumerator();
			Manager_Accessor.WriteOffUnitsAmount(expenditure, expenditureDate, markdownRate, price, incomesEnumerator);
			Assert.AreEqual(25, firstIncome.UnitsExpenditureAmount);
			Assert.AreEqual(25, secondIncome.UnitsExpenditureAmount);
			Assert.AreEqual(95.3475M, expenditure.MoneyAmount);
		}

		/// <summary>
		/// Gets the markup rate.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date for the markup rate.</param>
		/// <param name="dataContext">The data context.</param>
		/// <returns>Markup rate.</returns>
		private static decimal GetMarkupRate(UnitFund fund, DateTime date, UnitFundsEntities dataContext)
		{
			decimal markupRate =
				(from rate in dataContext.MarkupRates
				 where rate.FundId == fund.FundId && rate.Date < date
				 orderby rate.Date descending
				 select rate.Rate).First();
			return markupRate;
		}

		/// <summary>
		/// Gets the unit price.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date for the price.</param>
		/// <param name="dataContext">The data context.</param>
		/// <returns>The price of the unit fund.</returns>
		private static decimal GetUnitPrice(UnitFund fund, DateTime date, UnitFundsEntities dataContext)
		{
			decimal price =
				(from unitPrice in dataContext.UnitPrices
				 where unitPrice.FundId == fund.FundId && unitPrice.PriceDate <= date
				 orderby unitPrice.PriceDate descending
				 select unitPrice.Price).First();
			return price;
		}

		/// <summary>
		/// Gets the active incomes, i.e. incomes which units is not fully sold.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="dataContext">The data context.</param>
		/// <returns>Active incomes.</returns>
		private static List<Income> GetActiveIncomes(UnitFund fund, UnitFundsEntities dataContext)
		{
			var query =
				from selectedIncome in dataContext.Incomes
				join selectedOperation in dataContext.Operations
					on selectedIncome.IncomeId equals selectedOperation.OperationId
				where selectedIncome.FundId == fund.FundId && (selectedIncome.UnitsAmount != selectedIncome.UnitsExpenditureAmount || selectedIncome.UnitsExpenditureAmount == null)
				orderby selectedOperation.OperationDate descending
				select selectedIncome;
			List<Income> incomesToWriteOff = query.ToList();
			return incomesToWriteOff;
		}

		/// <summary>
		/// Gets the markdown rate.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date for markdown.</param>
		/// <param name="dataContext">The data context.</param>
		/// <returns>Markdown rate.</returns>
		private static decimal GetMarkdownRate(UnitFund fund, DateTime date, UnitFundsEntities dataContext)
		{
			decimal markdownRate =
				(from rate in dataContext.MarkdownRates
				 where rate.FundId == fund.FundId && rate.Date < date
				 orderby rate.Date
				 select rate.Rate).First();
			return markdownRate;
		}

		/// <summary>
		/// Buys the units.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date of the buying.</param>
		/// <param name="unitsCount">The units count.</param>
		private static void BuyUnits(UnitFund fund, DateTime date, decimal unitsCount)
		{
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Operation operation = new Operation();
				operation.OperationDate = date;
				operation.OperationId = Guid.NewGuid();
				operation.OperationTypeId = (int)OperationType.Income;
				Income income = new Income();
				income.FundId = fund.FundId;
				income.IncomeId = operation.OperationId;
				decimal price = GetUnitPrice(fund, date, dataContext);
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Unit Price = '{0}'", price));
				decimal markupRate = GetMarkupRate(fund, date, dataContext);
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Markup Rate = '{0}'", markupRate));
				decimal unitsCost = (decimal)unitsCount * price;
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Units Cost = '{0}'", unitsCost));
				decimal markupValue = unitsCost * (decimal)markupRate;
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Markup Value = '{0}'", markupValue));
				income.MoneyAmount = unitsCost + markupValue;
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Income Money Amount = '{0}'", income.MoneyAmount));
				income.UnitsAmount = unitsCount;
				Trace.TraceInformation(string.Format(CultureInfo.InvariantCulture, "Income Units Amount = '{0}'", income.UnitsAmount));
				dataContext.Operations.AddObject(operation);
				dataContext.Incomes.AddObject(income);
				dataContext.SaveChanges();
			}
		}

		/// <summary>
		/// Sells the units.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date of selling.</param>
		/// <param name="unitsCount">The units count.</param>
		private static void SellUnits(UnitFund fund, DateTime date, decimal unitsCount)
		{
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				Operation operation = new Operation();
				operation.OperationDate = date;
				operation.OperationId = Guid.NewGuid();
				operation.OperationTypeId = (int)OperationType.Expenditure;
				Expenditure expenditure = new Expenditure();
				expenditure.FundId = fund.FundId;
				expenditure.ExpenditureId = operation.OperationId;
				decimal price = GetUnitPrice(fund, date, dataContext);
				decimal markdownRate = GetMarkdownRate(fund, date, dataContext);
				expenditure.UnitsAmount = unitsCount;
				List<Income> incomesToWriteOff = GetActiveIncomes(fund, dataContext);
				decimal incomeMoneyToWriteOff = 0;
				decimal unitsCost = (decimal)unitsCount * price;
				while (unitsCount != 0)
				{
					Income incomeToWriteOff = incomesToWriteOff[0];
					decimal unitsRemainder = incomeToWriteOff.UnitsAmount - (incomeToWriteOff.UnitsExpenditureAmount ?? 0);
					if (unitsRemainder < unitsCount)
					{
						incomeToWriteOff.UnitsExpenditureAmount = incomeToWriteOff.UnitsAmount;
						unitsCount -= unitsRemainder;
						incomeMoneyToWriteOff += (decimal)(unitsRemainder * incomeToWriteOff.UnitsAmount) / incomeToWriteOff.MoneyAmount;
						incomesToWriteOff.RemoveAt(0);
					}
					else
					{
						incomeToWriteOff.UnitsExpenditureAmount = (incomeToWriteOff.UnitsExpenditureAmount ?? 0) + unitsCount;
						incomeMoneyToWriteOff += (decimal)unitsCount * incomeToWriteOff.MoneyAmount / (decimal)incomeToWriteOff.UnitsAmount;
						unitsCount = 0;
					}
				}
				decimal markdownValue = unitsCost * (decimal)markdownRate;
				decimal profit = unitsCost - markdownValue - incomeMoneyToWriteOff;
				decimal tax = 0;
				if (profit > 0)
				{
					tax = profit * decimal.Parse(ConfigurationManager.AppSettings["TaxRate"], CultureInfo.InvariantCulture);
				}
				expenditure.MoneyAmount = unitsCost - markdownValue - tax;
				dataContext.Operations.AddObject(operation);
				dataContext.Expenditures.AddObject(expenditure);
				dataContext.SaveChanges();
			}
		}

		/// <summary>
		/// Sets the unit price.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The price date.</param>
		/// <param name="price">The desired price.</param>
		private static void SetUnitPrice(UnitFund fund, DateTime date, decimal price)
		{
			UnitPrice unitPrice = new UnitPrice();
			unitPrice.FundId = fund.FundId;
			unitPrice.Price = price;
			unitPrice.PriceDate = date;
			unitPrice.UnitPriceId = Guid.NewGuid();
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				dataContext.UnitPrices.AddObject(unitPrice);
				dataContext.SaveChanges();
			}
		}

		/// <summary>
		/// Adds the markdown rate.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date of markdown.</param>
		/// <param name="value">The value of markdown.</param>
		/// <returns>Markdown rate instance.</returns>
		private static MarkdownRate AddMarkdownRate(UnitFund fund, DateTime date, decimal value)
		{
			MarkdownRate result = new MarkdownRate();
			result.Date = date;
			result.FundId = fund.FundId;
			result.MarkdownRateId = Guid.NewGuid();
			result.Rate = value;
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				dataContext.MarkdownRates.AddObject(result);
				dataContext.SaveChanges();
			}
			return result;
		}

		/// <summary>
		/// Adds the markup rate.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <param name="date">The date of markup rate.</param>
		/// <param name="value">The value of markdown.</param>
		/// <returns>Markup rate instance.</returns>
		private static MarkupRate AddMarkupRate(UnitFund fund, DateTime date, decimal value)
		{
			MarkupRate result = new MarkupRate();
			result.Date = date;
			result.FundId = fund.FundId;
			result.MarkupRateId = Guid.NewGuid();
			result.Rate = value;
			using (UnitFundsEntities dataContext = new UnitFundsEntities())
			{
				dataContext.MarkupRates.AddObject(result);
				dataContext.SaveChanges();
			}
			return result;
		}

		/// <summary>
		/// Adds the predefined markup rate for unit fund.
		/// </summary>
		/// <param name="fund">The unit fund.</param>
		/// <returns>Markup rate instance.</returns>
		private static MarkupRate AddMarkupRate(UnitFund fund)
		{
			return AddMarkupRate(fund, new DateTime(2010, 10, 19), 0.015M);
		}

		/// <summary>
		/// Adds the random unit fund to the company.
		/// </summary>
		/// <param name="company">The company.</param>
		/// <returns>Unit fund instance.</returns>
		private static UnitFund AddUnitFund(ManagementCompany company)
		{
			return AddUnitFund(company, 365);
		}

		/// <summary>
		/// Adds the unit fund.
		/// </summary>
		/// <param name="company">The company.</param>
		/// <param name="markdownPeriod">The markdown period.</param>
		/// <returns>Unit fund instance.</returns>
		private static UnitFund AddUnitFund(ManagementCompany company, int markdownPeriod)
		{
			UnitFund result = new UnitFund();
			result.CompanyId = company.CompanyId;
			result.FundId = Guid.NewGuid();
			result.FundName = "FundName";
			result.MarkdownPeriod = markdownPeriod;
			Manager.AddUnitFund(result);
			return result;
		}

		/// <summary>
		/// Adds the random management company.
		/// </summary>
		/// <returns>Management company instance.</returns>
		private static ManagementCompany AddManagementCompany()
		{
			ManagementCompany result = new ManagementCompany();
			result.CompanyId = Guid.NewGuid();
			result.CompanyName = "CompanyName";
			Manager.AddManagementCompany(result);
			return result;
		}

		/// <summary>
		/// Asserts the exception.
		/// </summary>
		/// <param name="action">The action.</param>
		/// <param name="expectedExceptionType">Expected type of the exception.</param>
		private static void AssertException(Action action, Type expectedExceptionType)
		{
			bool exceptionThrown = false;
			try
			{
				action();
			}
			catch (Exception ex)
			{
				exceptionThrown = true;
				Assert.IsInstanceOfType(ex, expectedExceptionType);
			}
			Assert.IsTrue(exceptionThrown);
		}
	}
}
